﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AMS.DD35.Common;
using AMS.DD35.DbLoader.DD35Service;
using System.ServiceModel;
using AMS.Core;

namespace AMS.DD35.DbLoader.DataAccess
{
    class CharacterProxy : ICharacterService
    {
        private DD35MasterServiceClient _dd35service;

        public CharacterProxy()
        {
            RestartConnection();
        }

        public CharacterData GetCharacterData(long characterID, object sessionVariable)
        {
            try
            {
                return _dd35service.GetCharacterData(characterID, sessionVariable);
            }
            catch (FaultException)
            {
                RestartConnection();
                ExceptionHandler.Exception("failed to get character data");
                return null;
            }
            // consider use of FaultException<T> in order to not break connection?
        }

        public void SaveCharacter(CharacterData characterData, object sessionVariable)
        {
            try
            {
                _dd35service.SaveCharacter(characterData, sessionVariable);
            }
            catch (FaultException)
            {
                RestartConnection();
                ExceptionHandler.Exception("failed to save character data");
            }
            // consider use of FaultException<T> in order to not break connection?
        }

        private void RestartConnection()
        {
            _dd35service = new DD35MasterServiceClient("WS2007HttpBinding_IDD35MasterService");
        }
    }
}
